package com.woniuxy.movice.handler;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.woniuxy.movice.dto.Round;
import com.woniuxy.movice.dto.RoundDate;
import com.woniuxy.movice.service.RoundService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Data;

@Data
@Controller
@RequestMapping("/round")
@Api("场次信息")
public class RoundController {

	@Resource
	private RoundService roundService;
	
	@ApiOperation(value="通过电影id，影院id查询电影场次",notes="传入电影院id，电影id")
	@RequestMapping(value="/showRound",method=RequestMethod.POST)
	@ResponseBody
	public List<Round> showRound(Round round){
		Calendar c = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
		try {
			Date start = sdf.parse(round.getStart());
			String startTime=new SimpleDateFormat("yyyy-MM-dd").format(start); 
			c.setTime(start); 
			int day=c.get(Calendar.DATE); 
			c.set(Calendar.DATE,day+1); 
			String endTime=new SimpleDateFormat("yyyy-MM-dd").format(c.getTime()); 
			round.setEnd(endTime);
			round.setStart(startTime);
			round.setNow(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");
		List<Round> rounds = roundService.showRound(round);
		for (Round r : rounds) {
			String time = timeFormat.format(r.getPstart());
			r.setTime(time);
			r.setLeaveTime(timeFormat.format(r.getPend()));
		}
		return rounds;
	}
	
	@ApiOperation(value="通过电影id，影院id查询电影时间",notes="传入电影院id，电影id")
	@RequestMapping(value="/showRoundDate",method=RequestMethod.POST)
	@ResponseBody
	public List<RoundDate> showRoundDate(Round round){
		SimpleDateFormat dateFormat = new SimpleDateFormat("y年M月d日");
		SimpleDateFormat monthFormat = new SimpleDateFormat("M月d日");
		round.setNow(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
		List<RoundDate> rds = roundService.showRoundDate(round);
		for (RoundDate roundDate : rds) {
			try {
				Date rDate = dateFormat.parse(roundDate.getRoundDate());
				roundDate.setRoundMonth(monthFormat.format(rDate));
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		return rds;
	}

}
